<?php
namespace Home\Controller;


/**
 * 后台用户控制器
 */
class MemberController extends BaseController {

    public $userapi = object;

    /**
     *  构造
     */
    public function __construct()
    {
        parent::__construct();
        $this -> userapi = A('Home/MemberApi');
    }
    
    
    /**
     * 用户管理首页
     */
    public function index(){
        $nickname       =   I('nickname');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }
        $list   = $this->flylists('Member', $map);
        
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '用户信息';
        $this->display();
    }
    
    /**
     * 用户行为列表
     */
    public function action(){
        //获取列表数据
        $list   =   $this->flylists("ActionLog", array('status'=>array('gt',-1)), true, '', 'id desc');
        foreach($list as $k => $v){
            $list[$k]['remark'] = strlen($list[$k]['remark'])>120 ? substr($list[$k]['remark'],0,120).'...' : $list[$k]['remark'];
            $list[$k]['createTimeShow'] = fdate($v['create_time']);
            $listModeTmp = getActionMode($v);
            $list[$k]['ActionName'] = $listModeTmp[0];
            $list[$k]['ActionType'] = $listModeTmp[1];
            $list[$k]['nickname'] = $listModeTmp[2];
            $list[$k]['StatusShow'] = $listModeTmp[3];
        } 
        // 记录当前列表页的cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        $this->assign('_list', $list);
        $this->meta_title = '用户行为';
        $this->display();
    }
    
    /**
     * 删除用户行为
     */
    public function delAction(){
        M('ActionLog') -> where(array('id'=>I('id'))) -> save(array('status'=>-1));
        $this->success('删除成功！', U('Home/member/action'));
    }

    /**
     * 清空用户行为
     */
    public function clearAction(){
        M('ActionLog') -> where(array('status'=>1)) -> save(array('status'=>-1));
        $this->success('清空成功！', U('Home/member/action'));
    }
    
    /**
     * 会员状态修改
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                //记录行为
                action_log('PC', 'Member_forbiduser', UID, UID);
                //
                $this->forbid('Member', $map );
                break;
            case 'resumeuser':
                //记录行为
                action_log('PC', 'Member_resumeuser', UID, UID);
                //
                $this->resume('Member', $map );
                break;
            case 'deleteuser':
                //记录行为
                action_log('PC', 'Member_deleteuser', UID, UID);
                //
                $this->delete('Member', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

    /**
     * 用户信息修改
     */
    public function edit($username = '', $password = '', $repassword = '', $email = ''){
        if(IS_POST){
            //记录行为
            action_log('PC', 'Member_edit', UID, UID);
            //
            //init
            $nickname = $username;
            $uid = I("uid");
            /* 检测 */
            empty($nickname) && $this->error('请输入昵称');
            empty($repassword) && $this->error('请输入确认密码');
            if($password != $repassword){
                $this->error('密码和重复密码不一致！');
            }
            if($password == "InitialPassword"){
                D('Member')->where(array('uid'=>$uid))->save(array('nickname'=>$nickname));
            }else{
                D('Member')->where(array('uid'=>$uid))->save(array('nickname'=>$nickname));
                D('UcenterMember') -> where( array('id' => $uid) ) 
                                   -> save( array('password' => think_ucenter_md5($password, UC_AUTH_KEY)) );
            }
            $this->success('用户操作成功！',U('index'));
        } else {
            //user info 
            $info = M('Member') -> where(array('uid'=>I("uid"))) -> find();
            $this -> assign('info' , $info); 
            $this -> assign('formSubmit' , U('edit')); 
            $this -> meta_title = '编辑用户'; 
            $this -> display('add'); 
        }
    }

    /**
     * 新增用户页面
     */
    public function add($username = '', $password = '', $repassword = '', $email = ''){
        if(IS_POST){
            //记录行为
            action_log('PC', 'Member_add', UID, UID);
            //
            /* 检测密码 */
            if($password != $repassword){
                $this->error('密码和重复密码不一致！');
            }
            /* 调用注册接口注册用户 */
            $uid = $this -> userapi -> register($username, $password);
            if(0 < $uid){ //注册成功
                $user = array('uid' => $uid, 'nickname' => $username, 'status' => 1);
                if(!M('Member')->add($user)){
                    $this->error('用户添加失败！');
                } else {
                    $this->success('用户添加成功！',U('index'));
                }
            } else { //注册失败，显示错误信息
                $this->error($this->showRegError($uid));
            }
        } else {
            $this->meta_title = '新增用户';
            $this->assign('formSubmit' , U('add'));
            $this->display();
        }
    }

    /**
     * 获取用户注册错误信息
     * @param  integer $code 错误编码
     * @return string        错误信息
     */
    private function showRegError($code = 0){
        switch ($code) {
            case -1:  $error = '用户名长度必须在16个字符以内！'; break;
            case -2:  $error = '用户名被禁止注册！'; break;
            case -3:  $error = '用户名被占用！'; break;
            case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
            case -5:  $error = '邮箱格式不正确！'; break;
            case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
            case -7:  $error = '邮箱被禁止注册！'; break;
            case -8:  $error = '邮箱被占用！'; break;
            case -9:  $error = '手机格式不正确！'; break;
            case -10: $error = '手机被禁止注册！'; break;
            case -11: $error = '手机号被占用！'; break;
            default:  $error = '未知错误';
        }
        return $error;
    }



}
